﻿/* 
 * Copyright 2010 Justin Pealing
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace Minnow.Web.Http
{
    /// <summary>
    /// Implementation of <see cref="Minnow.Web.Http.IHttpRequest" /> implemented by an
    /// underlying <see cref="System.Net.HttpListenerRequest" />
    /// </summary>
    public class ListenerHttpRequest : MarshalByRefObject, IHttpRequest
    {
        private HttpListenerRequest request;

        /// <summary>
        /// Initialises a new instance of ListenerHttpRequest for a given HttpListenerRequest
        /// </summary>
        /// <param name="Request">HttpListenerRequest to wrap</param>
        /// <exception cref="System.ArgumentNullException">
        /// Request is null
        /// </exception>
        public ListenerHttpRequest(HttpListenerRequest Request)
        {
            if (Request == null)
            {
                throw new ArgumentNullException("Request is null");
            }

            this.request = Request;
        }

        #region IHttpRequest Members

        public string[] AcceptTypes
        {
            get
            {
                return this.request.AcceptTypes;
            }
        }

        public int ClientCertificateError
        {
            get
            {
                return this.request.ClientCertificateError;
            }
        }

        public Encoding ContentEncoding
        {
            get
            {
                return this.request.ContentEncoding;
            }
        }

        public long ContentLength
        {
            get
            {
                return this.request.ContentLength64;
            }
        }

        public string ContentType
        {
            get
            {
                return this.request.ContentType;
            }
        }

        public CookieCollection Cookies
        {
            get
            {
                return this.request.Cookies;
            }
        }

        public bool HasEntityBody
        {
            get
            {
                return this.request.HasEntityBody;
            }
        }

        public NameValueCollection Headers
        {
            get
            {
                return this.request.Headers;
            }
        }

        public string HttpMethod
        {
            get
            {
                return this.request.HttpMethod;
            }
        }

        public Stream InputStream
        {
            get
            {
                return this.request.InputStream;
            }
        }

        public bool IsAuthenticated
        {
            get
            {
                return this.request.IsAuthenticated;
            }
        }

        public bool IsLocal
        {
            get
            {
                return this.request.IsLocal;
            }
        }

        public bool IsSecureConnection
        {
            get
            {
                return this.request.IsSecureConnection;
            }
        }

        public bool KeepAlive
        {
            get
            {
                return this.request.KeepAlive;
            }
        }

        public IPEndPoint LocalEndPoint
        {
            get
            {
                return this.request.LocalEndPoint;
            }
        }

        public Version ProtocolVersion
        {
            get
            {
                return this.request.ProtocolVersion;
            }
        }

        public NameValueCollection QueryString
        {
            get
            {
                return this.request.QueryString;
            }
        }

        public string RawUrl
        {
            get
            {
                return this.request.RawUrl;
            }
        }

        public System.Net.IPEndPoint RemoteEndPoint
        {
            get
            {
                return this.request.RemoteEndPoint;
            }
        }

        public Guid RequestTraceIdentifier
        {
            get
            {
                return this.request.RequestTraceIdentifier;
            }
        }

        public string ServiceName
        {
            get
            {
                return this.request.ServiceName;
            }
        }

        public System.Net.TransportContext TransportContext
        {
            get
            {
                return this.request.TransportContext;
            }
        }

        public Uri Url
        {
            get
            {
                return this.request.Url;
            }
        }

        public Uri UrlReferrer
        {
            get
            {
                return this.request.UrlReferrer;
            }
        }

        public string UserAgent
        {
            get
            {
                return this.request.UserAgent;
            }
        }

        public string UserHostAddress
        {
            get
            {
                return this.request.UserHostAddress;
            }
        }

        public string UserHostName
        {
            get
            {
                return this.request.UserHostName;
            }
        }

        public string[] UserLanguages
        {
            get
            {
                return this.request.UserLanguages;
            }
        }

        public IAsyncResult BeginGetClientCertificate(AsyncCallback requestCallback, object state)
        {
            return this.request.BeginGetClientCertificate(requestCallback, state);
        }

        public X509Certificate2 EndGetClientCertificate(IAsyncResult asyncResult)
        {
            return this.request.EndGetClientCertificate(asyncResult);
        }

        public X509Certificate2 GetClientCertificate()
        {
            return this.request.GetClientCertificate();
        }

        #endregion
    }
}